package com.sg.service.biz.pbff.nb;

import com.sg.common.util.RedisUtil;
import com.sg.dto.integration.framework.ClearTokenFromRedisReqDto;
import com.sg.dto.integration.framework.ClearTokenFromRedisRespDto;
import com.wicket.okrcomponent.common.annotations.BanAuto;
import org.apache.skywalking.apm.toolkit.trace.Tag;
import org.apache.skywalking.apm.toolkit.trace.Tags;
import org.apache.skywalking.apm.toolkit.trace.Trace;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @author ： 手工接入方法
 * @version 1.0
 * @since 2022/5/28 15:32
 */
@Service
public class NbUser {
    @Resource
    private RedisUtil redisClient;

    /**
     * code:batchClearTokenFromRedis
     * name:D-Redis批量清空用户Token
     * desc:undefined
     * gen by moon at 2022/7/4 下午7:01:27
     **/
    @Trace(operationName = "D-Redis批量清空用户Token")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @BanAuto
    public BatchClearTokenFromRedisRespDto batchClearTokenFromRedis(BatchClearTokenFromRedisReqDto reqDto) {
        if (reqDto.getBeginMemberList() == null || reqDto.getBeginMemberList().isEmpty()) {
            return new BatchClearTokenFromRedisRespDto();
        }
        for (String member : reqDto.getBeginMemberList()) {
            ClearTokenFromRedisReqDto req = new ClearTokenFromRedisReqDto();
            req.setOriginalRoleMemberId(member);
            clearTokenFromRedis(req);
        }
        return new BatchClearTokenFromRedisRespDto();
    }

    /**
     * code:batchClearUserInfoFromRedis
     * name:Redis批量清空用户信息
     * desc:undefined
     * gen by moon at 2022/7/4 下午7:01:29
     **/
    @Trace(operationName = "Redis批量清空用户信息")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @BanAuto
    public BatchClearUserInfoFromRedisRespDto batchClearUserInfoFromRedis(BatchClearUserInfoFromRedisReqDto reqDto) {
        if (reqDto.getBeginMemberList() == null || reqDto.getBeginMemberList().isEmpty()) {
            return new BatchClearUserInfoFromRedisRespDto();
        }
        for (String member : reqDto.getBeginMemberList()) {
            redisClient.delete("userbase-" + member);
            redisClient.delete("inductionInfo-" + member);
        }
        return new BatchClearUserInfoFromRedisRespDto();
    }

    @BanAuto
    public ClearTokenFromRedisRespDto clearTokenFromRedis(ClearTokenFromRedisReqDto reqDto) {
        String token = (String) redisClient.get("token-" + reqDto.getOriginalRoleMemberId());
        if (token == null || token.isEmpty()) {
            return new ClearTokenFromRedisRespDto();
        }
        redisClient.delete(token);
        redisClient.delete("token-" + reqDto.getOriginalRoleMemberId());
        return new ClearTokenFromRedisRespDto();
    }
    //手工接入方法
}
